﻿using Eholly.Database;
using Eholly.Database.Ef;
using Eholly.Hosting;
using Eholly.Logger;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Eholly.Framework.Database.Ef.Sqlite.Test
{
    /// <summary>
    /// 表示适用于Winform的主机
    /// </summary>
    /// <creator>marc</creator>
    public class WinFormAppHost<TForm> : AppHostBase
        where TForm : Form
    {
        /// <inheritdoc cref="WinFormAppHost{TForm}"/>
        /// <param name="loggerManager"><inheritdoc/></param>
        public WinFormAppHost(ILoggerManager loggerManager) : base(loggerManager) { }

        /// <inheritdoc cref="WinFormAppHost{TForm}"/>
        /// <param name="args"><inheritdoc/></param>
        /// <param name="loggerManager"><inheritdoc/></param>
        public WinFormAppHost(string[] args, ILoggerManager loggerManager) : base(args, loggerManager) { }

        /// <inheritdoc cref="WinFormAppHost{TForm}"/>
        /// <param name="configuration"><inheritdoc/></param>
        /// <param name="loggerManager"><inheritdoc/></param>
        public WinFormAppHost(HostConfiguration configuration, ILoggerManager loggerManager) : base(configuration, loggerManager)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                // 调用支持 EventLog 的代码
            }
        }

        /// <inheritdoc/>
        protected override void ConfigureJsonFiles(IConfigurationBuilder configuration)
        {
            base.ConfigureJsonFiles(configuration);
        }

        /// <inheritdoc/>
        protected override void ConfigureServices(IServiceCollection services)
        {
            base.ConfigureServices(services);

            //注入数据库驱动
            services.AddScoped<IDbContextProvider<TestSqliteDbContext>, DbContextProvider<TestSqliteDbContext>>((provider =>
            {
                var connectionString = Configuration.GetConnectionString("SqlServerConnection");
                var context = new TestSqliteDbContext(connectionString);

                return new DbContextProvider<TestSqliteDbContext>(context);
            }));

            //注入仓储
            services.AddScoped<IRepository<DataTransferRecordEntity, long>, EfRepository<TestSqliteDbContext, DataTransferRecordEntity, long>>();
            services.AddScoped<IRepository<ApiRequestRecordEntity, long>, EfRepository<TestSqliteDbContext, ApiRequestRecordEntity, long>>();
            services.AddScoped<IRepository<ProtocolRecordEntity, long>, EfRepository<TestSqliteDbContext, ProtocolRecordEntity, long>>();
            //注入工作单元
            services.AddScoped<IUnitOfWork, UnitOfWork<TestSqliteDbContext>>();

            services.AddTransient<TForm>();
        }

        /// <inheritdoc/>
        public override void Run()
        {
            Application.Run(ServiceProvider.GetRequiredService<TForm>());

            Logger.LogInformation($"应用程序启动完成");
        }
    }
}
